<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="请输入用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="请输入用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入驻家乡关键词" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="入驻家乡关键词" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="贡献值行为" prop="ruleId">
        <el-select v-model="queryParams.ruleId" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.con_value_behavior" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="贡献值数区间" prop="minNum">
        <el-row>
          <el-col :span="11">
            <el-input v-model="queryParams.minNum" placeholder="贡献值数区间" clearable></el-input>
          </el-col>
          <el-col :span="2">
            <div style="text-align: center;color:#DCDFE6;">—</div>
          </el-col>
          <el-col :span="11">
            <el-input v-model="queryParams.maxNum" placeholder="贡献值数区间" clearable></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="时间区间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <el-table :data="orderheadList" style="width: 100%">
      <el-table-column label="用户昵称" prop="nickName">
      </el-table-column>
      <el-table-column label="用户ID" prop="userId">
      </el-table-column>
      <el-table-column label="入驻家乡" prop="hometownAddress">
      </el-table-column>
      <el-table-column label="贡献值行为" prop="ruleId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_value_behavior" :value="scope.row.ruleId" />
        </template>
      </el-table-column>
      <el-table-column label="获得贡献值" prop="num">
      </el-table-column>
      <el-table-column label="获得时间" prop="createTime">
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="title" :visible.sync="open" @close="cancel" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="输入用户手机号" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="贡献值行为" prop="ruleId">
          <el-select v-model="form.ruleId" placeholder="请选择贡献值行为">
            <el-option v-for="dict in ruleList" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="获得贡献值" prop="addNum">
          <el-input v-model.number="form.addNum"></el-input>
          <!-- <el-input type="number" v-model.number="form.addNum" placeholder="请输入贡献值" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    listContribute,
    pushContribute
  } from "@/api/store/coin";

  export default {
    name: "Orderhead",
    dicts: ['con_value_behavior', ],
    data() {

      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('贡献值不能为空'));
        }
        if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value > 1000||value<1) {
              callback(new Error('1-1000之间'));
            } else {
              callback();
            }
          }
      };

      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单表头表格数据
        orderheadList: [],
        // 弹出层标题
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nickName: null,
          keyword: null,
          userId: null,
          source: null,
          minNum: null,
          maxNum: null,
          startDate: null,
          endDate: null,
        },
        // 表单参数
        form: {
          "ruleId": "",
          "userPhone": "",
          "addNum": null,
          "remark": ""
        },
        ruleList: [{
          value: 15,
          label: '意见反馈'
        }],
        // 表单校验
        rules: {
          userPhone: [{
            required: true,
            message: "请输入用户手机号",
            trigger: ["blur", "change"]
          }],
          ruleId: [{
            required: true,
            message: "请选择贡献值行为",
            trigger: ["blur", "change"]
          }],
          addNum: [{ validator: checkAge, trigger: 'blur' }]
        },
        dateRange: [],
        title: "贡献值",
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getStatus(res) {
        return res === '0' ?
          '待付款' :
          res === '5' ?
          '待发货' :
          res === '1' ?
          '待发货' :
          res === '6' ?
          '已发货' :
          res === '2' ?
          '已发货' :
          res === '3' ?
          '完成' :
          res === '4' ?
          '售后' :
          res === '7' ?
          '完成' :
          res === '8' ?
          '退款' :
          res === '9' ?
          '退货退款' :
          res === '10' ?
          '交易关闭' :
          '其他'
      },
      /** 查询订单表头列表 */
      getList() {
        let params = {
          // 用户昵称
          nickName: this.queryParams.nickName,
          // 用户id
          userId: this.queryParams.userId,
          // 入驻家乡关键词
          keyword: this.queryParams.keyword,
          // 贡献值行为
          ruleId: this.queryParams.ruleId,
          // 贡献值数区间
          minNum: this.queryParams.minNum,
          maxNum: this.queryParams.maxNum,
          // 时间区间
          startDate: this.dateRange[0],
          endDate: this.dateRange[1],
          pageSize: this.queryParams.pageSize,
          pageNum: this.queryParams.pageNum
        }
        listContribute(params).then(res => {
          if (res.code == '200') {
            this.orderheadList = res.rows
          }
          this.total = res.total
        });
      },
      // 表单重置
      reset() {
        this.form = {
          orderSerial: null,
          userId: null,
          addressId: null,
          totalAmount: null,
          couponFlag: null,
          coupon: null,
          actualAmount: null,
          emsFlag: null,
          emsFreight: null,
          orderStatus: null,
          successFlag: null,
          paySerial: null,
          orderTime: null,
          payTime: null,
          payMethods: null,
          deliveryMethod: null,
          orderRemark: null,
          invoiceFlag: null,
          delFlag: null,
          crTime: null,
          upTime: null,
          headFeild1: null,
          headFeild2: null,
          headFeild3: null,
          headFeild4: null,
          headFeild5: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.queryParams.maxNum = null
        this.queryParams.ruleId = null
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.orderSerial)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let data = {
              "ruleId": this.form.ruleId,
              "userPhone": this.form.userPhone,
              "addNum": this.form.addNum,
            }
            pushContribute(data).then(response => {
              this.open = false
              this.getList();
              this.$modal.msgSuccess("成功");
            });
          }
        });
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      handleAdd() {
        this.open = true
      },
    }
  };
</script>
<style>
  .br /deep/ .el-descriptions__header {
    display: none !important;
  }

  .ec-input-range-divider {
    width: 10px;
    height: 2px;
    background-color: #333;
    margin: 0 auto;
  }
</style>
